package com.isunland.managesystem.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.MyCircle;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/3/21 13:37
 */
public class MyCircleMultiListAdapter extends ArrayAdapter<MyCircle> {

  /**
   * ViewHolder class for layout.<br />
   * <br />
   * Auto-created on 2016-02-17 15:12:50 by Android Layout Finder
   * (http://www.buzzingandroid.com/tools/android-layout-finder)
   */

  private Map<String, Boolean> isCheckMap = new HashMap<>();

  private static class ViewHolder {
    public final LinearLayout rootView;
    final TextView tvNameSearchPerson;
    final TextView tvDepartmentSearchPerson;
    final CheckBox cbCheckApplyScale;

    private ViewHolder(LinearLayout rootView, TextView tvNameSearchPerson,
        TextView tvDepartmentSearchPerson, CheckBox cbCheckApplyScale) {
      this.rootView = rootView;
      this.tvNameSearchPerson = tvNameSearchPerson;
      this.tvDepartmentSearchPerson = tvDepartmentSearchPerson;
      this.cbCheckApplyScale = cbCheckApplyScale;
    }

    public static ViewHolder create(LinearLayout rootView) {
      TextView tvNameSearchPerson = (TextView) rootView.findViewById(R.id.tv_name_searchPerson);
      TextView tvDepartmentSearchPerson =
          (TextView) rootView.findViewById(R.id.tv_department_searchPerson);
      CheckBox cbCheckApplyScale = (CheckBox) rootView.findViewById(R.id.cb_checkApplyScale);
      return new ViewHolder(rootView, tvNameSearchPerson, tvDepartmentSearchPerson,
          cbCheckApplyScale);
    }
  }

  @NonNull @Override
  public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_add_applyscale, parent, false);
      vh = ViewHolder.create((LinearLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    MyCircle item = getItem(position);

    vh.tvNameSearchPerson.setText(item != null ? item.getCirclename() : "");
    vh.tvDepartmentSearchPerson.setText(item != null ? item.getRegDate() : "");

    vh.cbCheckApplyScale.setTag(item != null ? item.getId() : "");
    // 找到需要选中的条目
    if (isCheckMap != null && isCheckMap.containsKey(item != null ? item.getId() : "")) {
      vh.cbCheckApplyScale.setChecked(isCheckMap.get(item != null ? item.getId() : ""));
    } else {
      vh.cbCheckApplyScale.setChecked(false);
    }

    vh.cbCheckApplyScale.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      private String id;

      @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        id = buttonView.getTag().toString();
        if (isChecked) {
          // 将选中的放入hashmap中
          isCheckMap.put(id, true);
        } else {
          // 取消选中的则剔除
          isCheckMap.remove(id);
        }
      }
    });

    return vh.rootView;
  }

  public boolean isCheckedItem(String jobNO) {
    return isCheckMap != null && isCheckMap.containsKey(jobNO);
  }

  private LayoutInflater mInflater;

  // Constructors
  public MyCircleMultiListAdapter(Context context, List<MyCircle> objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }
}

